<?php
declare(strict_types=1);

namespace App\ZxAdmin\Sys\Stat;

use App\Controller;

// hook controller_zx_admin_sys_server_use.php

/**
 * @title   系统状态
 * @auth    true
 * @login   true
 * @menu    true
 * @component   other/server.vue
 * @icon    el-icon-menu
 * @active  true
 * @affix   false
 * @rank    1
 */
class Server extends Controller
{
    // hook controller_zx_admin_sys_server_public_start.php


    // hook controller_zx_admin_sys_server_public_end.php

    /**
     * @title   下发列表数据
     * @rank    73
     */
    public function action_Index_GET()
    {
        // hook controller_zx_admin_sys_server_index_start.php

        $stat = \Swoole\Coroutine::stats();
        $server = $this->http_server->stats();
        $redisInfo = $this->SysUser->CacheInfo();
        $mysqlInfo = $this->SysUser->show_status();
        $_table = $_ENV['Table'];
        $table = [];
        foreach ($_table as $k => $row) {
            $table[] = [
                'table' => $k,
                'size' => $row->size,
                'count' => $row->count(),
                'memorySize' => humansize($row->memorySize),
            ];
        }
        $mysqlInfo = arrlist_key_values($mysqlInfo,'Variable_name','Value');

        $data=[
            'stat' => [
                ['k' => '当前 reactor 事件数量', 'v' => ''.$stat['event_num']],
                ['k' => '当前监听信号的数量', 'v' => ''.$stat['signal_listener_num']],
                ['k' => '异步 IO 任务数量', 'v' => ''.$stat['aio_task_num']],
                ['k' => '异步 IO 工作线程数量', 'v' => ''.$stat['aio_worker_num']],
                ['k' => '每个协程的 C 栈大小', 'v' => ''.$stat['c_stack_size']],
                ['k' => '协程数量协程/峰值', 'v' => $stat['coroutine_num'] . ' / ' . $stat['coroutine_peak_num']],
                ['k' => '最后创建协程的 id', 'v' => ''.$stat['coroutine_last_cid']],
            ],
            'server' => [
                ['k' => '服务启动的时间', 'v' => date('Y-m-d H:i:s', $server['start_time'])],
                ['k' => '当前连接的数量', 'v' => ''.$server['connection_num']],
                ['k' => '接受了多少个连接', 'v' => ''.$server['accept_count']],
                ['k' => '关闭的连接数量', 'v' => ''.$server['close_count']],
                ['k' => '空闲的进程数W/T', 'v' => ''.$server['idle_worker_num'] . ' / ' . $server['task_idle_worker_num']],
                ['k' => '当前正在排队的任务数', 'v' => ''.$server['tasking_num']],
                ['k' => '收到的请求次数', 'v' => ''.$server['request_count']],
            ],
            'sys'=>[
                ['k'=>'在线客户','v'=>count($this->http_server->connections).'/'.$this->http_server->setting['max_connection']],
                ['k'=>'运行环境','v'=>PHP_OS_FAMILY .' PHP v'.PHP_VERSION.' Swoole v'.SWOOLE_VERSION],
                ['k'=>'最大协程','v'=>$this->http_server->setting['max_coroutine']],
                ['k'=>'协程开关','v'=>' Worker '.($this->http_server->setting['enable_coroutine']?'开' : '关').' Task '.($this->http_server->setting['task_enable_coroutine']?'开' : '关')],
                ['k'=>'整数范围','v'=>PHP_INT_MIN.' ~ '.PHP_INT_MAX],
                ['k'=>'句柄限制','v'=>PHP_FD_SETSIZE],
                ['k'=>'服务监听','v'=>$this->http_server->host.':'.$this->http_server->port],
                ['k'=>'服务日志','v'=>$this->http_server->setting['log_file']. ' ('. humansize(filesize($this->http_server->setting['log_file'])). ')'],
                ['k'=>'代码目录','v'=>__APPDIR__. ' ('. dirSize(__APPDIR__). ')'],
                ['k'=>'Tmp日志','v'=>__TMPDIR__. ' ('. dirSize(__TMPDIR__). ')'],
                ['k'=>'日志目录','v'=>__LOGDIR__. ' ('. dirSize(__LOGDIR__). ')'],
                ['k'=>'统计目录','v'=>__CAHDIR__. 'Stat/ ('. dirSize(__CAHDIR__.'Stat'). ')'],
                ['k'=>'框架日志','v'=>__CAHDIR__. 'Service/ ('. dirSize(__CAHDIR__.'Service'). ')'],
                ['k'=>'扩展目录','v'=>__VENDIR__. ' ('. dirSize(__VENDIR__). ')'],
                ['k'=>'通讯协议','v'=>('HTTP协议 '.($this->http_server->setting['open_http_protocol'] ? '开' : '关')).(' WS协议 '.($this->http_server->setting['open_websocket_protocol'] ? '开' : '关'))],
                ['k'=>'使用内存','v'=>humansize(memory_get_peak_usage())],
                ['k'=>'输出限制','v'=>humansize($this->http_server->setting['buffer_output_size'])],
                ['k'=>'上传限制','v'=>humansize($this->http_server->setting['package_max_length'])],
                ['k'=>'守护模式','v'=>$this->http_server->setting['daemonize']?"开":"关"],
                ['k'=>'进程ID','v'=>'master_pid '.$this->http_server->master_pid.' manager_pid '.$this->http_server->manager_pid],
                ['k'=>'服务进程','v'=>'worker_num '.$this->http_server->setting['worker_num'].' task_worker_num'.$this->http_server->setting['task_worker_num']],
                ['k'=>'磁盘信息','v'=>  var_export(\Swoole\Coroutine\System::statvfs("/"),true)],
            ],
            'cpu'=> sys_cpu(),
            'mem'=> sys_mem(),
            'redis'=>[
                ['k'=>'Redis版本','v'=>$redisInfo['redis_version']],
                ['k'=>'当前客户端数','v'=>''.$redisInfo['connected_clients']],
                ['k'=>'内存占用','v'=>humansize($redisInfo['used_memory_rss'])],
                ['k'=>'内存峰值','v'=>humansize($redisInfo['used_memory'])],
                ['k'=>'内存碎片%','v'=>''.$redisInfo['mem_fragmentation_ratio']],
                ['k'=>'已连接总数','v'=>''.$redisInfo['total_connections_received']],
                ['k'=>'已执行命令','v'=>''.$redisInfo['total_commands_processed']],
                ['k'=>'每秒命令数','v'=>''.$redisInfo['instantaneous_ops_per_sec']],
                ['k'=>'查询成功数','v'=>''.$redisInfo['keyspace_hits']],
                ['k'=>'查询失败数','v'=>''.$redisInfo['keyspace_misses']],
            ],
            'mysql'=>[
                ['k'=>'当前连接数','v'=>''.$mysqlInfo['Threads_connected']],
                ['k'=>'激活线程数','v'=>''.$mysqlInfo['Threads_running']],
                ['k'=>'临时表文件','v'=>''.$mysqlInfo['Created_tmp_disk_tables']],
                ['k'=>'临时表内存','v'=>''.$mysqlInfo['Created_tmp_tables']],
                ['k'=>'慢查询','v'=>''.$mysqlInfo['Slow_queries']],
                ['k'=>'打开表数量','v'=>''.$mysqlInfo['Open_tables']],
                ['k'=>'Handler_read_first','v'=>''.$mysqlInfo['Handler_read_first']],
                ['k'=>'Handler_read_key','v'=>''.$mysqlInfo['Handler_read_key']],
                ['k'=>'执行请求个数','v'=>''.$mysqlInfo['Queries']],
                ['k'=>'立即获取锁数','v'=>''.$mysqlInfo['Table_locks_immediate']],
                ['k'=>'延迟获取锁数','v'=>''.$mysqlInfo['Table_locks_waited']],
                ['k'=>'执行查询数量','v'=>''.$mysqlInfo['Questions']],
                ['k'=>'SELECT 次数','v'=>''.$mysqlInfo['Com_select']],
                ['k'=>'INSERT 次数','v'=>''.$mysqlInfo['Com_insert']],
                ['k'=>'UPDATE 次数','v'=>''.$mysqlInfo['Com_update']],
                ['k'=>'DELETE 次数','v'=>''.$mysqlInfo['Com_delete']],
                ['k'=>'已经运行时间','v'=>''.formatTimeElapsed((int)$mysqlInfo['Uptime'])],
            ],
            'table'=>$table
        ];

        for ($i = 0; $i < 17; $i++) {
            $data['redis'][]=[
              'k'=>'DB'.$i,
              'v'=> ''.(!empty($redisInfo['db'.$i])?$redisInfo['db'.$i]:'')
            ];
        }
        //var_dump(\Swoole\Coroutine\System::exec("df -h"));
        // hook controller_zx_admin_sys_server_index_end.php
        $this->response('0000', $data);
    }

    // hook controller_zx_admin_sys_server_end.php
}

?>